package tum0r.misc;

import java.lang.reflect.Field;

/**
 * 工程: JavaTools<br>
 * 包: tum0r.misc<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/8/21 22:22<br>
 * <br>
 */
public class Copy<E> {
    public E deepCopy()
            throws InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException {
        Class<E> c = (Class<E>) this.getClass();
        Object temp = c.newInstance();
        E res = c.cast(temp);
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            field.set(res, field.get(this));
        }
        return res;
    }

    public E copy() {
        Class<E> c = (Class<E>) this.getClass();
        E res = c.cast(this);
        return res;
    }
}
